-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
63 lines (56 loc) · 1.37 KB
/
Solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#define SIZE 100
int queue[SIZE], front = -1, rear = -1;
void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue is full!\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Inserted: %d\n", value);
}
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty!\n");
} else {
printf("Removed: %d\n", queue[front++]);
if (front > rear) front = rear = -1;
}
}
void display() {
if (front == -1) {
printf("Queue is empty!\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
int choice, value;
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}